package cn.vc.content.service.article;

import cn.vc.content.entity.article.ArticleReview;
import cn.vc.content.entity.video.VideoReview;
import cn.vc.content.mapper.article.ArticleReviewMapper;
import cn.vc.content.mapper.video.VideoReviewMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class ArticleReviewService {
    @Resource
    private ArticleReviewMapper articleReviewMapper;


    /**
     * 评论
     * @param articleReview
     * @return
     */
    public boolean review(ArticleReview articleReview) {
        return articleReviewMapper.insert(articleReview)==1;
    }


    /**
     * 查询评论回复及回复。。。
     * @param articleReviewList
     * @return
     */
    public List<ArticleReview> selectRecover(List<ArticleReview> articleReviewList) {
        for (ArticleReview articleReview : articleReviewList) {
            // 查询评论回复
            LambdaQueryWrapper<ArticleReview> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(ArticleReview::getReviewId, articleReview.getId());
            List<ArticleReview> articleReviewList1 = articleReviewMapper.selectList(lqw1);
            // 如果评论的回复为空，则无需查询该回复的回复
            if (articleReviewList1!=null) {
                selectRecover(articleReviewList1);
                // 反转list
                Collections.reverse(articleReviewList1);
                articleReview.setArticleRecoverList(articleReviewList1);
            }
        }
        return articleReviewList;
    }
    /**
     * 查询视频评论
     */
    public List<ArticleReview> selectReview(Long articleId) {
        // 查询视频回复
        LambdaQueryWrapper<ArticleReview> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ArticleReview::getArticleId, articleId);
        List<ArticleReview> articleReviewList = articleReviewMapper.selectList(lqw);
        // 反转list
        Collections.reverse(articleReviewList);

        articleReviewList = selectRecover(articleReviewList);

        return articleReviewList;
    }
}
